import tkinter.messagebox
from model.ui import MudUI
import tkinter
from model.player import Player
from model.map.xinshoucun import XinShouCun  # 新手村地图

# 创建全局玩家实例
player = Player("勇者", game_map=XinShouCun(), room_name="南村口")
# 设置初始地图
current_map = XinShouCun()
player.current_map = current_map

def handle_command(command: str):
    """处理游戏命令"""
    if command == "look":
        # 获取当前位置的描述
        room_desc = current_map.get_description(player.x, player.y)
        if not room_desc:
            room_desc = ui.create_styled_text(
                ("你环顾四周，", None),
                ("发现一个神秘的红色宝箱", "red"),
                ("。\n房间的墙壁上挂着", None),
                ("闪闪发光的蓝色符文", "blue"),
                ("。", None)
            )
        ui.update_room(room_desc)
        
        # 更新玩家状态
        status = ui.create_styled_text(
            ("生命值: ", None),
            (str(player.hp), "bold green"),
            (f"/{player.max_hp}\n", None),
            ("魔法值: ", None),
            (str(player.mp), "bold blue"),
            (f"/{player.max_mp}", None)
        )
        ui.update_status(status)
    elif command.startswith("go "):
        direction = command.split(" ")[1]
        # 尝试移动玩家
        if player.move(direction):
            ui.write_log(f"你向{direction}方向移动了。", style="yellow")
            # 移动成功后自动执行look命令查看新位置
            handle_command("look")
        else:
            ui.write_log("你不能往那个方向移动。", style="red")
    elif command == "help":
        ui.write_log("可用命令：\nlook - 查看周围\ngo <方向> - 移动(north/south/east/west)\nhelp - 显示帮助", style="green")
    else:
        ui.write_log("未知命令。输入 help 查看可用命令。", style="red")

def create_about_text():
    """创建关于信息的样式文本"""
    from rich.text import Text
    result = Text()
    segments = [
        ("项目名称：", None), ("福禄MUD游戏\n", "bold green"),
        ("版本：", None), ("1.0.0\n", "yellow"),
        ("发布日期：", None), ("2024年\n\n", "blue"),
        
        ("项目描述\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("这是一个基于Python开发的文字冒险游戏（MUD），以中国古典神话为背景，让玩家在充满仙侠色彩的虚拟世界中探索冒险。\n\n", None),
        
        ("主要功能\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("- ", None), ("丰富的剧情任务系统\n", "green"),
        ("- ", None), ("独特的修仙升级体系\n", "cyan"),
        ("- ", None), ("多样化的社交互动功能\n", "magenta"),
        ("- ", None), ("动态地图探索系统\n", "blue"),
        ("- ", None), ("实时战斗系统\n\n", "red"),
        
        ("技术栈\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("- Python 3.8+\n", "green"),
        ("- SQLite 数据库\n", "cyan"),
        ("- WebSocket 实时通信\n", "blue"),
        ("- 异步IO处理\n", "magenta"),
        ("- Textual TUI框架\n", "yellow"),
        ("- Rich 终端渲染\n\n", "red"),
        
        ("作者与维护\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("作者：", None), ("福禄开发团队\n", "green"),
        ("联系方式：", None), ("contact@fulu-mud.com\n\n", "blue"),
        
        ("版权信息\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("© 2024 福禄MUD开发组 版权所有\n\n", "cyan"),
        
        ("使用许可\n", "bold yellow"),
        ("-----------------\n", "dim"),
        ("本项目采用 ", None), ("MIT 许可证", "green"), ("。详细信息请参阅 ", None), ("LICENSE", "blue"), (" 文件。", None)
    ]
    for text, style in segments:
        result.append(text, style=style)
    return result

def handle_menu(menu_item: str):
    """处理菜单选择"""
    if str(menu_item) == "存档":
        ui.write_log("保存游戏...", style="blue")
    elif str(menu_item) == "地图":
        # 显示当前地图信息
        map_info = current_map.get_map_info() if hasattr(current_map, 'get_map_info') else "当前位置：新手村"
        ui.write_log(map_info, style="cyan")
        # 显示ASCII地图
        if hasattr(current_map, 'get_ascii_map'):
            ascii_map = current_map.get_ascii_map()
            ui.update_status(ascii_map)
    elif str(menu_item) == "读档":
        ui.write_log("加载游戏...", style="blue")
    elif str(menu_item) == "退出":
        ui.write_log("正在退出游戏...", style="yellow")
        ui.exit()
    elif str(menu_item) == "属性":
        status_text = ui.create_styled_text(
            ("生命值: ", None), (str(player.hp), "bold green"), (f"/{player.max_hp}\n", None),
            ("魔法值: ", None), (str(player.mp), "bold blue"), (f"/{player.max_mp}\n", None),
            ("等级: ", None), (str(player.level), "yellow"), ("\n", None),
            ("经验: ", None), (str(player.exp), "cyan"), (f"/{player.level * 100}\n", None),
            ("力量: ", None), (str(player.strength), "red"), ("\n", None),
            ("敏捷: ", None), (str(player.agility), "green"), ("\n", None),
            ("智力: ", None), (str(player.intelligence), "blue"), ("\n", None),
            ("体力: ", None), (str(player.vitality), "magenta"), ("\n", None)
        )
        ui.update_status(status_text)
    elif str(menu_item) == "关于":
        ui.update_status(create_about_text())
    else:
        ui.write_log(f"选择菜单：{menu_item}", style="dim blue")

# 创建UI实例
ui = MudUI(
    initial_room="欢迎来到福禄 MUD！\n这是一个神奇的世界，等待着你的探索。\n输入 help 查看可用命令。",
    initial_status=create_about_text(),
    initial_message="游戏启动完成，祝您游戏愉快！"
)

# 注册事件处理函数
ui.on_command = handle_command
ui.on_menu = handle_menu

# 启动UI主循环
if __name__ == "__main__":
    ui.run() 